/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess.Utilities
*        文件名:             DbHelper.cs
*        创建系统时间:       2012/11/2 19:07:00
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess.Utilities
{
    using System;
    using System.Data;
    using System.Data.Common;
    
    using ECP.Utilities;

    public class DbHelper
    {
        private static readonly IDbHelper dbHelper = DbHelperFactory.GetHelper();
        private static DbProviderFactory factory = null;

        private DbHelper()
        {
        }

        public static int ExecuteNonQuery(string sqlQuery)
        {
            return ExecuteNonQuery(CommandType.Text, sqlQuery, null);
        }

        public static int ExecuteNonQuery(string sqlQuery, DbParameter[] dbParameters)
        {
            return ExecuteNonQuery(CommandType.Text, sqlQuery, dbParameters);
        }

        public static int ExecuteNonQuery(CommandType CommandType, string sqlQuery, DbParameter[] dbParameters)
        {
            int num = 0;
            dbHelper.Open(DbHelperFactory.DbConnectionString);
            num = dbHelper.ExecuteNonQuery(CommandType, sqlQuery, dbParameters);
            dbHelper.Close();
            return num;
        }

        public static int ExecuteProcedure(string procedureName)
        {
            return ExecuteProcedure(procedureName, null);
        }

        public static int ExecuteProcedure(string procedureName, DbParameter[] dbParameters)
        {
            int num = 0;
            dbHelper.Open(DbHelperFactory.DbConnectionString);
            num = dbHelper.ExecuteProcedure(procedureName, dbParameters);
            dbHelper.Close();
            return num;
        }

        public static DataTable ExecuteProcedureForDataTable(string procedureName, string tableName, DbParameter[] dbParameters)
        {
            dbHelper.Open(DbHelperFactory.DbConnectionString);
            DataTable table = dbHelper.ExecuteProcedureForDataTable(procedureName, tableName, dbParameters);
            dbHelper.Close();
            return table;
        }

        public static object ExecuteScalar(string sqlQuery)
        {
            return ExecuteScalar(CommandType.Text, sqlQuery, null);
        }

        public static object ExecuteScalar(string sqlQuery, DbParameter[] dbParameters)
        {
            return ExecuteScalar(CommandType.Text, sqlQuery, dbParameters);
        }

        public static object ExecuteScalar(CommandType CommandType, string sqlQuery, DbParameter[] dbParameters)
        {
            object obj2 = null;
            dbHelper.Open(DbHelperFactory.DbConnectionString);
            obj2 = dbHelper.ExecuteScalar(CommandType, sqlQuery, dbParameters);
            dbHelper.Close();
            return obj2;
        }

        public static DataTable Fill(DataTable dataTable, string sqlQuery)
        {
            return Fill(dataTable, CommandType.Text, sqlQuery, null);
        }

        public static DataSet Fill(DataSet dataSet, string sqlQuery, string tableName)
        {
            dbHelper.Open(DbHelperFactory.DbConnectionString);
            dbHelper.Fill(dataSet, sqlQuery, tableName);
            dbHelper.Close();
            return dataSet;
        }

        public static DataTable Fill(DataTable dataTable, string sqlQuery, DbParameter[] dbParameters)
        {
            return dbHelper.Fill(dataTable, CommandType.Text, sqlQuery, dbParameters);
        }

        public static DataSet Fill(DataSet dataSet, string sqlQuery, string tableName, DbParameter[] dbParameters)
        {
            return Fill(dataSet, CommandType.Text, sqlQuery, tableName, dbParameters);
        }

        public static DataTable Fill(DataTable dataTable, CommandType CommandType, string sqlQuery, DbParameter[] dbParameters)
        {
            dbHelper.Open(DbHelperFactory.DbConnectionString);
            dbHelper.Fill(dataTable, CommandType, sqlQuery, dbParameters);
            dbHelper.Close();
            return dataTable;
        }

        public static DataSet Fill(DataSet dataSet, CommandType CommandType, string sqlQuery, string tableName, DbParameter[] dbParameters)
        {
            dbHelper.Open(DbHelperFactory.DbConnectionString);
            dbHelper.Fill(dataSet, CommandType, sqlQuery, tableName, dbParameters);
            dbHelper.Close();
            return dataSet;
        }

        public static string GetDBNow()
        {
            return dbHelper.GetDBNow();
        }

        public static string GetParameter(string parameter)
        {
            return dbHelper.GetParameter(parameter);
        }

        public static DbParameter MakeInParam(string targetFiled, object targetValue)
        {
            return dbHelper.MakeInParam(targetFiled, targetValue);
        }

        public static DbParameter MakeParam(string paramName, DbType DbType, int size, ParameterDirection Direction, object Value)
        {
            return dbHelper.MakeParam(paramName, DbType, size, Direction, Value);
        }

        public static DbParameter[] MakeParameters(string targetFiled, object targetValue)
        {
            return dbHelper.MakeParameters(targetFiled, targetValue);
        }

        public static DbParameter[] MakeParameters(string[] targetFileds, object[] targetValues)
        {
            return dbHelper.MakeParameters(targetFileds, targetValues);
        }

        public static string PlusSign(params string[] values)
        {
            return dbHelper.PlusSign(values);
        }

        public static string SqlSafe(string value)
        {
            return dbHelper.SqlSafe(value);
        }

        public static bool TestConn(DataBaseType dataBaseType, string dataBase, string userName, string password, string workstation, bool trustLink)
        {
            bool flag = false;
            IDbHelper helper = null;
            helper = new OleDbHelper(BaseConfiguration.Instance.GetOleDbConnection(dataBaseType, dataBase, userName, password, workstation, trustLink));
            try
            {
                if (helper.GetDbConnection().State == ConnectionState.Closed)
                {
                    helper.Open();
                }
                helper.Close();
                flag = true;
            }
            catch (Exception exception)
            {
                throw exception;
            }
            finally
            {
                helper = null;
            }
            return flag;
        }

        public static DbProviderFactory Factory
        {
            get
            {
                if (factory == null)
                {
                    factory = dbHelper.Instance();
                }
                return factory;
            }
        }
    }
}